package GameDemo.FightState.AllState;

import GameDemo.Actor;
import com.EventSystem;
import com.Log;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.function.Consumer;

public abstract class BaseFightState {
    protected EventSystem.EventReg eventReg;
    Actor owner;
    
    public BaseFightState(Actor owner) {
        this.owner = owner;
        eventReg = new EventSystem.EventReg(owner.eventMgr);
        registerEvents();
    }

    private void registerEvents() {
        try {
            Method[] methods = this.getClass().getDeclaredMethods();
            for (Method method : methods) {
                EventRegister annotation = method.getAnnotation(EventRegister.class);
                if (annotation != null) {
                    EventSystem.EventType eventType = annotation.value();
                    method.setAccessible(true);
                    // 使用反射创建方法引用
                    eventReg.Register(eventType, (Consumer<Object>) (data) -> {
                        try {
                            method.invoke(this, data);
                        } catch (Exception e) {
                            Log.Debug("Error invoking event handler: " + e.getMessage());
                        }
                    });
                }
            }
        } catch (Exception e) {
            Log.Debug("Error registering events: " + e.getMessage());
        }
    }

    public void Destroy() {
        eventReg.Destroy();
    }

    public <T> void Register(EventSystem.EventType type, Consumer<T> callback) {
        eventReg.Register(type, callback);
    }

    // 叠加buff
    public void Stack(ArrayList<Object> cfg) {
        Log.Debug("default Stack");
    }
}
